package com.shenyue.nbtownlet.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.shenyue.nbtownlet.R;
import com.shenyue.nbtownlet.databinding.LeaderIntroduceItemBinding;
import com.shenyue.nbtownlet.util.GlideUtil;
import com.shenyue.nbtownlet.util.StringUtil;

public class LeaderIntroduceListViewAdapter extends BaseAdapter {
    private Context mContext;
    private JSONArray items = new JSONArray();

    public LeaderIntroduceListViewAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (convertView == null) {
            LeaderIntroduceItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.leader_introduce_item, viewGroup, false);
            convertView = binding.getRoot();
            viewHolder = new ViewHolder(binding);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        LeaderIntroduceItemBinding root = viewHolder.getBinding();
        JSONObject item = items.getJSONObject(position);
        root.REALNAME.setText(item.getString("REALNAME"));
        root.MOBILE.setText(item.getString("MOBILE"));
        root.Duty.setText(StringUtil.getValue(item.getString("Duty")));
        root.RESUME.setText("\u3000\u3000" + StringUtil.getValue(item.getString("RESUME")));
        root.INTRODUCTION.setText(StringUtil.getValue(item.getString("INTRODUCTION")));
        String HEADPORTRAIT = item.getString("HEADPORTRAIT");
        GlideUtil.loadImg(mContext, HEADPORTRAIT, root.HEADPORTRAIT);

        return convertView;
    }

    public void addItems(JSONArray array) {
        items.addAll(array);
        notifyDataSetChanged();
    }

    public void clear() {
        items.clear();
        notifyDataSetChanged();
    }

    private class ViewHolder {
        private LeaderIntroduceItemBinding binding;

        public ViewHolder(LeaderIntroduceItemBinding binding) {
            this.binding = binding;
        }

        public LeaderIntroduceItemBinding getBinding() {
            return binding;
        }
    }
}
